package com.sxt.practiceapplication.pager;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.tab.QMUITabBuilder;
import com.qmuiteam.qmui.widget.tab.QMUITabIndicator;
import com.qmuiteam.qmui.widget.tab.QMUITabSegment;
import com.sxt.practiceapplication.R;
import com.sxt.practiceapplication.base.BasePager;
import com.sxt.practiceapplication.common.Urls;
import com.sxt.practiceapplication.databinding.IndexContentBinding;
import com.sxt.practiceapplication.entity.course.CourseType;
import com.sxt.practiceapplication.entity.course.data.CourseTypeListData;
import com.sxt.practiceapplication.pager.indextabdetialpager.IndexTabDetailPager;
import com.sxt.practiceapplication.utils.GsonBinder;
import com.sxt.practiceapplication.utils.okhttp.DefaultRealCallback;
import com.sxt.practiceapplication.utils.okhttp.OkHttpUtils;
import com.sxt.practiceapplication.utils.okhttp.WrapResponse;
import com.sxt.practiceapplication.utils.okhttp.url.URL;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class IndexPager extends BasePager {

    private ViewPager indexContentPager;

    private IndexContentBinding indexContentBinding;

    private ArrayList<IndexTabDetailPager> indexTabDetailPagers;

    private QMUITabSegment tabPagerIndicator;
    private QMUITabBuilder tabBuilder;

    public IndexPager(Context context) {
        super(context);
    }


    @Override
    public void initContent(FrameLayout content) {
        content.removeAllViews();
        indexContentBinding = IndexContentBinding.inflate(LayoutInflater.from(context));
        indexContentPager = indexContentBinding.indexContentPager;
        tabPagerIndicator = indexContentBinding.tabPagerIndicator;
        tabBuilder = tabPagerIndicator.tabBuilder();
        content.addView(indexContentBinding.getRoot());

        initIndicator();
    }

    private void initIndicator() {
        tabPagerIndicator.setEnabled(true);
        tabPagerIndicator.setupWithViewPager(indexContentPager);
        tabPagerIndicator.setMode(QMUITabSegment.MODE_SCROLLABLE);
        int space = QMUIDisplayHelper.dp2px(context, 16);
        tabPagerIndicator.setIndicator(new QMUITabIndicator(
                QMUIDisplayHelper.dp2px(context, 2), false, true));
        tabPagerIndicator.setItemSpaceInScrollMode(space);
        tabPagerIndicator.setPadding(space, 0, space, 0);
    }

    @Override
    public void doInitData() {
        //设置标题
        headTitle.setText(R.string.index_title);
        //联网请求数据
        OkHttpUtils.getInstance().get(new URL(Urls.COURSE_TYPE_URL), new DefaultRealCallback(this, context) {
            @Override
            public void onResponse(Call call, WrapResponse response) {

                CourseTypeListData courseTypeListData = GsonBinder.toObj(response.getBody(), CourseTypeListData.class);
                List<CourseType> courseTypes = courseTypeListData.getData().getResults();
                indexTabDetailPagers = new ArrayList<>();
                for (int i = 0; i < courseTypes.size(); i++) {
                    indexTabDetailPagers.add(new IndexTabDetailPager(context, courseTypes.get(i)));
                }
                //绑定数据
                indexContentPager.setAdapter(new PagerAdapter() {
                    @Override
                    public int getCount() {
                        return indexTabDetailPagers.size();
                    }

                    @Override
                    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                        return view == object;
                    }

                    @NonNull
                    @Override
                    public Object instantiateItem(@NonNull ViewGroup container, int position) {
                        IndexTabDetailPager indexTabDetailPager = indexTabDetailPagers.get(position);
                        View rootView = indexTabDetailPager.rootView;
                        container.addView(rootView);
                        indexTabDetailPager.initData();
                        return rootView;
                    }

                    @Override
                    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                        container.removeView((View) object);
                    }

                    @Nullable
                    @Override
                    public CharSequence getPageTitle(int position) {
                        return indexTabDetailPagers.get(position).courseType.getType();
                    }
                });
            }
        });
    }
}
